package algorithm;
//Used in Sentinel Inc.'s Defensive Driver Prototype
//Programmer: John Casey Marr
//Programmer:
//Programmer: 
//Class CS 411 Professional Workforce Development

import gui.obu.ObuDrivingDisplayPanel;
import gui.obu.ObuDrivingDisplayPanelImpl;
import gui.obu.alerts.SeatBeltAlert;

import java.util.Calendar;

import database.OBUdata;
import database.OBUdb;

import objects.GeneralLog;
import objects.OBUentry;

public class Seat_Belt_Detection {

	private boolean engagement;
	private Calendar seat_now;
	private long seat_start;
	private long seat_stop;
	private boolean on=true;
	private OBUentry myEntry;
	
	//GUI Stuff
	private ObuDrivingDisplayPanel window;
	
	public void setWindow(ObuDrivingDisplayPanel window)
	{
		this.window=window;
	}

	public Seat_Belt_Detection() {

	        //Is the Seatbelt engaged? Collected from OBU Database
	        engagement=false;
	        //Calendar
	        seat_now=Calendar.getInstance();
	        seat_start=0;
	        seat_stop=0;
	        //Has the seatbelt been on?
	        on=true;
		myEntry = new OBUentry();
	}
	
	public void compute(OBUdata runtimedb, OBUdb obudatabase, Integer i,Integer driverID,Integer dbSize)
	{
		//Step 1: Collect OBU Information on seat belt engagement
		//engagement=OBU.seatbelt();
		myEntry.setID(i);
		try {
			runtimedb.getOBUEntry(myEntry);
		} catch (java.sql.SQLException mysqlex) {
			System.out.println("Cannot get this entry");
			return;
		}
		
		//Step 2:  Alert driver if the seatbelt is off
		if(!myEntry.getSeatBelt() && on)
		{
			//Alert
			if(window==null)
			{
				System.out.println("Can Not Add Seat Belt Alert");
			}
			else
			{
				if(!((ObuDrivingDisplayPanelImpl)window).getDisplay().isDisposed()) {
					((ObuDrivingDisplayPanelImpl)window).getDisplay().asyncExec(new Runnable() {
						public void run() {
							((ObuDrivingDisplayPanelImpl) window).addAlert(new SeatBeltAlert());
						}
					});
				}
			}
			seat_now.setTimeInMillis(System.currentTimeMillis());
			seat_start=seat_now.getTimeInMillis();
			on=false;
						
		}
		
		//Step 3:  Save duration of seat belt infraction when driver engages seatbelt
		if(myEntry.getSeatBelt() && !on)
		{
			//Save to driver profile
                        //Driverprofile.seatbelt(seat_start,seat_stop);
                        //Set on to true;

                        seat_now.setTimeInMillis(System.currentTimeMillis());
                        seat_stop=seat_now.getTimeInMillis();

			GeneralLog seatlog = new GeneralLog();
			seatlog.setType(1);
			seatlog.setStudentID(driverID);
			seatlog.setLocationID(1);
			seatlog.setDuration(new Integer((int)seat_stop - (int)seat_start));

			try {
				obudatabase.addGeneralLog(seatlog);
			} catch (java.sql.SQLException sqlex) {
				System.out.println("Coult not write seat belt log to DB!!!!");
			}

			on=true;
			
			if(!((ObuDrivingDisplayPanelImpl)window).getDisplay().isDisposed()) {
				((ObuDrivingDisplayPanelImpl)window).getDisplay().asyncExec(new Runnable() {
					public void run() {
						SeatBeltAlert alert = new SeatBeltAlert();
						alert.setTrue();
						((ObuDrivingDisplayPanelImpl) window).addAlert(alert);
					}
				});
			}
		}
		if(i==dbSize)
		{
			seat_now.setTimeInMillis(System.currentTimeMillis());
            seat_stop=seat_now.getTimeInMillis();

            GeneralLog seatlog = new GeneralLog();
            seatlog.setType(1);
            seatlog.setStudentID(driverID);
            seatlog.setLocationID(1);
            seatlog.setDuration(new Integer((int)seat_stop - (int)seat_start));

            try {
            	obudatabase.addGeneralLog(seatlog);
            } catch (java.sql.SQLException sqlex) {
            	System.out.println("Coult not write seat belt log to DB!!!!");
            }

            on=true;
            
            if(!((ObuDrivingDisplayPanelImpl)window).getDisplay().isDisposed()) {
				((ObuDrivingDisplayPanelImpl)window).getDisplay().asyncExec(new Runnable() {
					public void run() {
						SeatBeltAlert alert = new SeatBeltAlert();
						alert.setTrue();
						((ObuDrivingDisplayPanelImpl) window).addAlert(alert);
					}
				});
			}
		}
	}
	
}

